package com.buercorp.wangyu.core1.ch12.threads.threadsPool;

import java.util.concurrent.*;

/**
 * 通过 Executors 创建线程池
 * Executors 是一个线程池的工具类
 * 提供了很多静态方法用于返回不同特点的线程池对象
 * @author liyiyu
 */
public class ThreadRunnableText3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 通过 ThreadPoolExecutor创建线程池
          */
//        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, // 核心
//                5,
//                6,
//                TimeUnit.SECONDS, // 单位时间
//                new ArrayBlockingQueue<>(4), // 任务队列
//                Executors.defaultThreadFactory(), // 创建线程工厂
//                new ThreadPoolExecutor.AbortPolicy() // 任务拒绝策略
//        );

        // 通过 Executors 创建线程池
        ExecutorService pool = Executors.newFixedThreadPool(3);

        // 使用线程处理多任务
        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));
        Future<String> f4 = pool.submit(new MyCallable(400));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
    }
}
